﻿// -----------------------------------------------------------------------
// <copyright file="commClient.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace TCPIPClient
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net.Sockets;
    using System.Threading;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class commClient
    {

        public delegate void delegateTransmissionReceived(string text);
        public event delegateTransmissionReceived TransmissionReceived;

        private string _ID = "";

        System.Net.Sockets.TcpClient clientSocket = null;
        NetworkStream serverStream = null;
        string readData = null;
        private Thread ctThread = null;

        public commClient()
        {
            clientSocket = new System.Net.Sockets.TcpClient();
            serverStream = default(NetworkStream);
        }

        public bool Connect(string IPAddr, int Port, string ID)
        {
            try
            {
                this._ID = ID;

                //readData = "Conected to Server ...";
                //msg();
                clientSocket.Connect(IPAddr, Port);
                serverStream = clientSocket.GetStream();
                byte[] outStream = System.Text.Encoding.ASCII.GetBytes(this._ID + "$");
                serverStream.Write(outStream, 0, outStream.Length);
                serverStream.Flush();

                ctThread = new Thread(getMessage);
                ctThread.Start();

                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public bool Disconnect()
        {
            clientSocket.Close();
            this.ctThread.Abort();
            return true;
        }

        public bool IsConnected
        {
            get { return clientSocket.Connected; }
        }

        public bool Send(string text)
        {
            try
            {
                byte[] outStream = System.Text.Encoding.ASCII.GetBytes(text + "$");
                serverStream.Write(outStream, 0, outStream.Length);
                serverStream.Flush();

                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        private void getMessage()
        {
            try
            {
                while (true)
                {
                    serverStream = clientSocket.GetStream();
                    int buffSize = 0;
                    byte[] inStream = new byte[10025];
                    //buffSize = clientSocket.ReceiveBufferSize;
                    buffSize = 1024;
                    int readBytes = serverStream.Read(inStream, 0, buffSize);
                    string returndata = System.Text.Encoding.ASCII.GetString(inStream);
                    readData = "" + returndata;

                    //msg();
                    if (this.TransmissionReceived != null)
                    {
                        this.TransmissionReceived(readData);
                    }
                }
            }
            catch (Exception e)
            {
                clientSocket.Close();
            }
        }

        /*
        private void msg()
        {
            if (this.InvokeRequired)
                this.Invoke(new MethodInvoker(msg));
            else
            {
                //textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + readData;
                if (this.TransmissionRFeceived != null)
                {
                    this.TransmissionRFeceived(readData);
                }
            }
        }
        */

    }
}
